<?php
namespace app\index\controller;
use think\View;
use think\Controller;
use think\Db;
use app\index\model\User;
class Regist extends Controller{

  public function index(){
  	$view = new View();
  	return $view->fetch('index');

  }
  
  //用户注册
  public function regist(){
  	 
    //实例化User
      $user = new User;
    //接收前端表单提交的数据
    $user->username = input('post.user_name');
    $user->password = input('post.user_password');
		$user->face = "img/".mt_rand(0,24).".gif";
		$data = input('request.captcha');
	       
    //进行规则验证
    $result = $this->validate(
      [
        'name' => $user->username,
        'password' => $user->password,
      ],
      [
        'name' => 'require|max:10',
        'password' => 'require',
      ]);
	  
    if (true !== $result) {
      $this->error($result);
    }
	
		if(!captcha_check($data)){
     	//验证失败
        return $this->error("验证码错误");
    };
    //写入数据库
    if ($user->save()) {
      return $this->success('注册成功',"login/index");
	  exit();
    } else {
      return $this->error('注册失败');
    }
    
    
  }
}